/**********************************************************************
 * Filename: Participacao.java
 * Created: 2012/04/23
 * @author Fernando Brito e Abreu
 **********************************************************************/
package org.quasar.copaPaises.businessLayer;

import java.util.Set;
import java.util.HashSet;

public class Participacao {
	public static Set<Participacao> allInstances = new HashSet<Participacao>();

	private int classificacao;
	private boolean organizador;
	private Campeonato participado;
	private Pais participante;
	private Set<Jogador> plantel = new HashSet<Jogador>();
	private Treinador treinador;

	/**********************************************************************
	 * Default constructor
	 **********************************************************************/
	public Participacao() {
		allInstances.add(this);
	}

	/**********************************************************************
	 * Parameterized constructor
	 * 
	 * @param classificacao
	 *            the classificacao to initialize
	 * @param organizador
	 *            the organizador to initialize
	 * @param participado
	 *            the participado to initialize
	 * @param participante
	 *            the participante to initialize
	 * @param plantel
	 *            the plantel to initialize
	 * @param treinador
	 *            the treinador to initialize
	 **********************************************************************/
	public Participacao(int classificacao, boolean organizador,
			Campeonato participado, Pais participante, Set<Jogador> plantel,
			Treinador treinador) {
		this.classificacao = classificacao;
		this.organizador = organizador;
		this.participado = participado;
		this.participante = participante;
		this.plantel = plantel;
		this.treinador = treinador;
		allInstances.add(this);
	}

	/**********************************************************************
	 * Standard attribute getter
	 * 
	 * @return the classificacao of the participacao
	 **********************************************************************/
	public int classificacao() {
		return classificacao;
	}

	/**********************************************************************
	 * Standard attribute setter
	 * 
	 * @param classificacao
	 *            the classificacao to set
	 **********************************************************************/
	public void setClassificacao(int classificacao) {
		this.classificacao = classificacao;
	}

	/**********************************************************************
	 * Standard attribute getter
	 * 
	 * @return the organizador of the participacao
	 **********************************************************************/
	public boolean organizador() {
		return organizador;
	}

	/**********************************************************************
	 * Standard attribute setter
	 * 
	 * @param organizador
	 *            the organizador to set
	 **********************************************************************/
	public void setOrganizador(boolean organizador) {
		this.organizador = organizador;
	}

	/**********************************************************************
	 * ASSOCIATIVE2MEMBER getter for Participacao[*] <-> Campeonato[1]
	 * 
	 * @return the participado of the participacao
	 **********************************************************************/
	public Campeonato participado() {
		return participado;
	}

	/**********************************************************************
	 * ASSOCIATIVE2MEMBER setter for Participacao[*] <-> Campeonato[1]
	 * 
	 * @param participado
	 *            the participado to set
	 **********************************************************************/
	public void setParticipado(Campeonato participado) {
		this.participado = participado;
	}

	/**********************************************************************
	 * ASSOCIATIVE2MEMBER getter for Participacao[*] <-> Pais[1]
	 * 
	 * @return the participante of the participacao
	 **********************************************************************/
	public Pais participante() {
		return participante;
	}

	/**********************************************************************
	 * ASSOCIATIVE2MEMBER setter for Participacao[*] <-> Pais[1]
	 * 
	 * @param participante
	 *            the participante to set
	 **********************************************************************/
	public void setParticipante(Pais participante) {
		this.participante = participante;
	}

	/**********************************************************************
	 * MANY2MANY getter for Participacao[*] <-> Set(Jogador)[*]
	 * 
	 * @return the plantel of the participacao
	 **********************************************************************/
	public Set<Jogador> plantel() {
		return plantel;
	}

	/**********************************************************************
	 * MANY2MANY setter for Participacao[*] <-> Set(Jogador)[*]
	 * 
	 * @param plantel
	 *            the plantel to set
	 **********************************************************************/
	public void setPlantel(Set<Jogador> plantel) {
		this.plantel = plantel;
	}

	/**********************************************************************
	 * ONE2MANY getter for Participacao[*] <-> Treinador[1]
	 * 
	 * @return the treinador of the participacao
	 **********************************************************************/
	public Treinador treinador() {
		return treinador;
	}

	/**********************************************************************
	 * ONE2MANY setter for Participacao[*] <-> Treinador[1]
	 * 
	 * @param treinador
	 *            the treinador to set
	 **********************************************************************/
	public void setTreinador(Treinador treinador) {
		this.treinador = treinador;
	}

	/**********************************************************************
	 * User-defined operation specified in SOIL/OCL
	 * 
	 * @param nome
	 *            the nome to set
	 * @param data_nasce
	 *            the data_nasce to set
	 * @param posicao
	 *            the posicao to set
	 * @param nacionalidade
	 *            the nacionalidade to set
	 * @param joga_clube
	 *            the joga_clube to set
	 * @param joga_pais
	 *            the joga_pais to set
	 **********************************************************************/
	public void adicionaJogador(String nome, String data_nasce,
			PosicaoJogador posicao, Pais nacionalidade, String joga_clube,
			Pais joga_pais) {

		CalendarDate dummy = new CalendarDate();
		CalendarDate data_nascimento = dummy.stringToDate(data_nasce);

		Clube clube = null;
		for (Clube c : Clube.allInstances)
			if (c.nome().equals(joga_clube))
				clube = c;

		if (clube == null) {
			clube = new Clube();
			clube.setNome(joga_clube);
			clube.setPais(joga_pais);
		}

		Jogador j = new Jogador(data_nascimento, nome, nacionalidade, posicao,clube);
		this.plantel().add(j);
	}

	/**********************************************************************
	 * User-defined operation specified in SOIL/OCL
	 **********************************************************************/
	public void organiza() {
		this.organizador = true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	/**********************************************************************
	 * Object serializer
	 **********************************************************************/
	public String toString() {
		return "Participacao [classificacao=" + classificacao
				+ ", organizador=" + organizador + ", participado="
				+ participado + ", participante=" + participante + ", plantel="
				+ plantel + ", treinador=" + treinador + "]\n";
	}

}
